package io.sage.modules.goods.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import io.sage.common.utils.DateUtils;
import io.sage.common.utils.R;
import io.sage.modules.goods.entity.BGoods;
import io.sage.modules.goods.entity.BGoodsRecord;
import io.sage.modules.goods.enums.GoodsRecordStatusEnum;
import io.sage.modules.goods.enums.GoodsRecordTypeStatusEnum;
import io.sage.modules.goods.enums.GoodsStatusEnum;
import io.sage.modules.goods.service.BGoodsRecordService;
import io.sage.modules.goods.service.BGoodsService;
import io.sage.modules.sys.shiro.ShiroUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 物品检查记录表(BGoodsRecord)表控制层
 *
 * @author zxh
 * @since 2019-08-13 11:30:00
 */
@RestController
@RequestMapping("goods/bgoodsrecord")
public class BGoodsRecordController {
    /**
     * 服务对象
     */
    @Resource
    private BGoodsRecordService bGoodsRecordService;
    /**
     * 服务对象
     */
    @Resource
    private BGoodsService bGoodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public BGoodsRecord selectOne(Integer id) {
        return this.bGoodsRecordService.queryById(id);
    }


    @PostMapping("savebGoodsRecord")
    public R saveRecord(@RequestBody  JSONObject jsonObject){
        JSONArray ids=jsonObject.getJSONArray("ids");
        for (int i = 0; i <ids.size() ; i++) {
            Integer id = Integer.parseInt(ids.getString(i));
            Integer status=jsonObject.getInteger("type");
            Long userId = ShiroUtils.getUserEntity().getUserId();
            BGoods goods = bGoodsService.selectByPrimaryKey(id);
            Date date=goods.getDueDate();
            goods.setDueDate(DateUtils.addDateDays(goods.getDueDate(),goods.getCheckTime()));
            goods.setGoodsStatus(status==0? GoodsStatusEnum.NORMAL.getCode():GoodsStatusEnum.BAD.getCode());
            Integer update=bGoodsService.updateByPrimaryKeySelective(goods);
            if(update>0){
                BGoodsRecord bGoodsRecord=new BGoodsRecord();
                bGoodsRecord.setCreateTime(new Date());
                bGoodsRecord.setGoodsId(goods.getId());
                bGoodsRecord.setIsDel(0);
                bGoodsRecord.setUserId(userId);
                bGoodsRecord.setStatus(status==0? GoodsRecordStatusEnum.NORMAL.getCode():GoodsRecordStatusEnum.BAD.getCode());
                bGoodsRecord.setRemark(jsonObject.getString("remark"));
                bGoodsRecord.setPic(jsonObject.getString("pic"));
                bGoodsRecord.setDamage(jsonObject.getInteger("damage"));
                if(date!=null){
                    bGoodsRecord.setType(new Date().compareTo(date)>=0?GoodsRecordTypeStatusEnum.NORMAL.getCode():GoodsRecordTypeStatusEnum.BAD.getCode());
                }
                bGoodsRecord=bGoodsRecordService.insert(bGoodsRecord);
            }

        }
        return R.ok();
    }

}